<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "bus_config".
 *
 * @property string $config_id
 * @property string $shop_id
 * @property integer $config_type
 * @property string $config_key
 * @property string $config_val
 * @property string $config_val2
 */
class BusConfig extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bus_config';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['shop_id', 'config_type', 'config_key', 'config_val'], 'required'],
            [['shop_id', 'config_type'], 'integer'],
            [['config_key', 'config_val', 'config_val2'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'config_id' => 'Config ID',
            'shop_id' => 'Shop ID',
            'config_type' => 'Config Type',
            'config_key' => 'Config Key',
            'config_val' => 'Config Val',
            'config_val2' => 'Config Val2',
        ];
    }
    /*模块交互接口  */
    /**
     * 增加卡项
     * @date: 2016-2-22 
     * @author: futao
     */
    public function addCard($type,$key,$val,$val2,$shopId){
    	if($key=='' || !in_array($type,[1,2,3,4,5,6]) || $val==''){
    		return false;
    	}
    	$configModel = new BusConfig();
    	$configModel->config_type = $type;
    	$configModel->config_key = $key;
    	$configModel->config_val = $val;
    	$configModel->config_val2 = $val2;
    	$configModel->shop_id =  $shopId;
    	if(!$configModel->save(false)){
    		return false;
    	}
    	return true;
    }
    /**
     * 编辑卡项
     * @date: 2016-2-22
     * @author: futao
     */
    public function updateCard($key,$val,$val2,$id,$shopId){
    	if($key=='' || $val=='' || $shopId==''){
    		return false;
    	}
    	$configModel = BusConfig::find()->where("shop_id = {$shopId} and config_id = {$id}")->one();
    	if(!$configModel){
    		return false;
    	}
    	$configModel->config_key = $key;
    	$configModel->config_val = $val;
    	if($val2!='')$configModel->config_val2 = $val2;
    	if(!$configModel->save(false)){
    		return false;
    	}
    	return true;
    }
    /*end  */
    /**
     * 初始化数据
     * @date: 2016-3-18 
     * @author: futao
     */
    public function initData()
    {
    	$userinfo = Yii::$app->userInfo;
    	$transaction = \Yii::$app->db->beginTransaction();
    	try {
    		$configModel = new BusConfig();
	    	$count = $this->find()
	    	->where("shop_id = {$userinfo->shop_id} and config_type = 6 and config_key = '规格'")
	    	->count();
	    	if($count==0){
	    		$data = $this->formatData(1, ['30g/瓶','50g/瓶','100g/瓶','30ml/瓶','50ml/瓶','100ml/瓶']);
	    		foreach ($data as $val){
	    			$specsModels = clone $configModel;
	    			$specsModels->config_type = $val['config_type'];
	    			$specsModels->shop_id = $val['shop_id'];
	    			$specsModels->config_key = $val['config_key'];
	    			$specsModels->config_val = $val['config_val'];
	    			$specsModels->insert(false);
	    		}
	    	}
	    	unset($data);
	    	$roomCount = $this->find()
	    	->where("shop_id = {$userinfo->shop_id} and config_type = 5 and config_key = '房间'")
	    	->count();
	    	if($roomCount==0){
	    		$data = $this->formatData(2, ['普通房间','VIP房间']);
	    		foreach ($data as $val){
	    			$roomModel = clone $configModel;
	    			$roomModel->config_type = $val['config_type'];
	    			$roomModel->shop_id = $val['shop_id'];
	    			$roomModel->config_key = $val['config_key'];
	    			$roomModel->config_val = $val['config_val'];
	    			$roomModel->insert(false);
	    		}
	    	}
	    	$transaction->commit();
	    	return true;
    	}catch (\Exception $e){
    		$transaction->rollBack();
    		return false;
    	}
    }
    /**
     * 格式化数据 
     * @date: 2016-3-18 
     * @author: futao
     * type  1 规格   2 房间
     */
    private function formatData($type,$data)
    {
    	$userinfo = Yii::$app->userInfo;
    	if($type==1){
    		foreach($data as $k=>$val){
    			$formatData[$k]['config_type'] = 6;
    			$formatData[$k]['shop_id'] = $userinfo->shop_id;
    			$formatData[$k]['config_key'] = '规格';
    			$formatData[$k]['config_val'] = $val;
    		}
    	}else{
    		foreach($data as $k=>$val){
    			$formatData[$k]['config_type'] = 5;
    			$formatData[$k]['shop_id'] = $userinfo->shop_id;
    			$formatData[$k]['config_key'] = '房间';
    			$formatData[$k]['config_val'] = $val;
    		}
    	}
    	return $formatData;
    }
    
    
}
